﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class Sys_syslog : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["tid"] == null)
            Response.Redirect("../Error/Error.htm");
        if (!IsPostBack)
        {
            BindGridView(true);
        }
    }

    protected void CheckAll(object sender, EventArgs e)
    {
        CheckBox cbx = (CheckBox)sender;
        foreach (GridViewRow gvr in GridView1.Rows)
        {
            CheckBox ch = (CheckBox)gvr.FindControl("ItemBox");
            ch.Checked = cbx.Checked;
        }
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindGridView(false);
    }

    protected void btnSeach_Click(object sender, ImageClickEventArgs e)
    {
        BindGridView(true);
    }

    /// <summary>
    /// 绑定gridview
    /// </summary>
    /// <param name="GetCount">是否获取总数</param>
    protected void BindGridView(bool GetCount)
    {
        StringBuilder where = new StringBuilder(" 1=1 ");
        if (this.txtName.Text.Trim() != string.Empty)
            where.Append(" and username='").Append(this.txtName.Text.Trim()).Append("'");
        else if (this.txtStart.Value.Trim() != string.Empty)
            where.Append(" and operatedate>='").Append(this.txtStart.Value.Trim()).Append("'"); 
        else if (this.txtName.Text.Trim() != string.Empty)
            where.Append(" and operatedate<='").Append(this.txtEnd.Value.Trim()).Append("'");
        else if (this.ddlUserType.SelectedValue != "99")
            where.Append(" and usertype=").Append(this.ddlUserType.SelectedValue.Trim());
        else if (this.ddlOperateType.SelectedValue!="99")
            where.Append(" and flag=").Append(this.ddlOperateType.SelectedValue.Trim());
        string field = "id,username,usertype,ip,browser,operate,operatedate,flag";
        PM.Code.Web.controlbind.BindGridViewPublicProc(GridView1, AspNetPager1, "Sys_Log", "id", field, "id desc", where.ToString(), GetCount);
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        Hashtable sqlstrs = new Hashtable();
        int keyid = -1;
        int i = 1;
        foreach (GridViewRow gvr in GridView1.Rows)
        {
            CheckBox check = (CheckBox)gvr.FindControl("ItemBox");
            if (check.Checked)
            {
                keyid = Int32.Parse(GridView1.DataKeys[gvr.DataItemIndex].Value.ToString().Trim());
                string param = "@mailid" + i.ToString();
                string sqlstr = "delete from Sys_Log where id=" + param;
                SqlParameter[] MailParms = new SqlParameter[1];
                MailParms[0] = new SqlParameter(param, keyid);
                sqlstrs.Add(sqlstr, MailParms);
                i++;
            }
        }
        if (i > 1)
        {
            PM.Code.DAL.DbHelper.ExecuteSqlTran(sqlstrs);
            BindGridView(true);
        }
    }
}
